//
//  XingchengDetailViewController.m
//  Travel
//
//  Created by lanou3g on 15/4/12.
//  Copyright (c) 2015年 &#21556;&#21697;&#27901;. All rights reserved.
//

#import "XingchengDetailViewController.h"
#import <AFHTTPRequestOperationManager.h>
#import <UIImageView+WebCache.h>

#import "xinchengDetailModel.h"
#import "XingchengDetailModel1.h"
#import "XingchengDetailModel2.h"

#import "XingChegDetalCell.h"
@interface XingchengDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

//设置头饰图
@property (nonatomic,strong)UIImageView *traImageView;
@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,retain)UIView *backView;


@property (nonatomic,strong)xinchengDetailModel *allMode;

@property (nonatomic,retain)UITableView *tableView;

@property (nonatomic,assign)NSInteger flag;
@end

@implementation XingchengDetailViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createTableView];
    [self requestAllData];
}

- (void)requestAllData
{
    NSString *str1 = [NSString stringWithFormat:@"%@",self.IDNumber];
    NSString *string = [NSString stringWithFormat:@"https://chanyouji.com/api/plans/%@.json?page=1",str1];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:string parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSLog(@"%@",responseObject);
        self.allMode = [[xinchengDetailModel alloc]init];
        [self.allMode setValuesForKeysWithDictionary:responseObject];
        [self.allMode getXingchengDetailModel1];
        for (XingchengDetailModel1 *mode1 in self.allMode.plan_days) {
            [mode1 getXingchengDetailModel2];
        }
      //  NSLog(@"!!!!!!!!!!!!!%@",self.allMode);
    
        
        UIImageView *headView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HIGHT * 0.3)];
       // NSLog(@"!!!!!%@",self.allMode.image_url);
        NSURL *urlimage = [NSURL URLWithString:self.allMode.image_url];
#pragma 占位图没加
        headView.userInteractionEnabled = YES;
        [headView sd_setImageWithURL:urlimage placeholderImage:nil];
        
        self.titleLabel = [[UILabel alloc]init];
        // self.traImageView.backgroundColor = [UIColor blackColor];
        self.titleLabel.numberOfLines = 0;
        self.titleLabel.font = [UIFont boldSystemFontOfSize:20];
        self.titleLabel.frame = CGRectMake(headView.frame.size.width / 10, headView.frame.size.height - 60 , headView.frame.size.width - 20,30);
        self.titleLabel.textColor = [UIColor whiteColor];
        self.titleLabel.text = self.allMode.name;
        
        self.backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, headView.frame.size.width, headView.frame.size.height)];
        self.backView.backgroundColor = [UIColor blackColor];
        self.backView.alpha = 0.2;
        
        [headView addSubview:self.backView];
        [headView addSubview:self.titleLabel];
        self.tableView.tableHeaderView = headView;
        
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"网络请求出错了");
    }];
    

}


- (void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 30)];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(view.frame.size.width * 1.3 /4, 0, 50, 30)];
    
     XingchengDetailModel1 *mod = [self.allMode.plan_days objectAtIndex:section];
    NSString *string = [NSString stringWithFormat:@"DAY%ld",section + 1];
    label.text = string;
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake( view.frame.size.width * 1.3 / 4 + 50, 0, view.frame.size.width * 2.7 /4 - 50, 30)];
    [view addSubview:label];
    [view addSubview:label2];
    label2.font = [UIFont systemFontOfSize:15];
   // label2.alpha = 0.7;
    label2.text = [[[mod.plan_nodes objectAtIndex:0] destination] objectForKey:@"name_zh_cn"];
    // label2.backgroundColor = [UIColor redColor];
    //label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor whiteColor];
    view.backgroundColor = [UIColor whiteColor];
    
    return view;
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.allMode.plan_days.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    XingchengDetailModel1 *mod = [self.allMode.plan_days objectAtIndex:section];
    
    return mod.plan_nodes.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XingchengDetailModel1 *mod = [self.allMode.plan_days objectAtIndex:indexPath.section];
    XingchengDetailModel2  *mode2 = [mod.plan_nodes objectAtIndex:indexPath.row];
    
    static NSString *ident = @"katc";
    XingChegDetalCell *cell = [tableView dequeueReusableCellWithIdentifier:ident];
    if (cell == nil) {
        cell = [[XingChegDetalCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ident];
    }
    
#pragma 占位图没加
    [cell.imageViewPhoto sd_setImageWithURL:[NSURL URLWithString:mode2.image_url] placeholderImage:nil];
    
    NSString *stt = [NSString stringWithFormat:@"第%ld站:%@",indexPath.row + 1,mode2.entry_name];
    cell.titleLabel.text = stt;
    cell.titleLabel.font = [UIFont boldSystemFontOfSize:17];
    cell.labelDes.text = mode2.tips;
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
    CGRect rect = [cell.labelDes.text boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    cell.labelDes.frame = CGRectMake(10, 240, self.view.frame.size.width - 20,rect.size.height);
    self.flag = cell.labelDes.frame.size.height + 240;
    cell.labelDes.numberOfLines = 0;
    cell.labelDes.font = [UIFont systemFontOfSize:15];
    [cell.contentView addSubview:cell.labelDes];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.flag + 15;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
